//
//  YKWiFiListVController.m
//  DoorBell
//
//  Created by Chun Hung Cheung on 2022/12/27.
//  Copyright © 2022 Chun Hung Cheung. All rights reserved.
//

#import "YKWiFiListVController.h"
#import "VL4WiFiCell.h"
@interface YKWiFiListVController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)UITableView *wifiListView;
@property(nonatomic,strong)UILabel *titlelabel;
@property(nonatomic,strong)UIButton *cancelButton;
@property(nonatomic,strong)UIButton *completeButton;
@end

@implementation YKWiFiListVController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = [UIColor whiteColor];
     self.bottomView.backgroundColor = [UIColor whiteColor];
     self.titlelabel.text =  NSLocalizedString(@"附近Wi-Fi", nil);
     self.wifiListView.backgroundColor = [UIColor whiteColor];
    [self.wifiListView reloadData];
    [self.cancelButton setTitleColor:[HexChangeRGB colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    [self.cancelButton setTitle: NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];

}

- (NSMutableArray*)selected_list_arrar
{
    if (!_selected_list_arrar) {
         _selected_list_arrar = [NSMutableArray array];
    }
    return _selected_list_arrar;;
}


- (UIView*)bottomView
{
    if (!_bottomView) {
         _bottomView = [[UIView alloc]init];
         _bottomView.layer.cornerRadius = 14;
         _bottomView.layer.masksToBounds = YES;
         _bottomView.userInteractionEnabled = YES;
        [self.view addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.height.mas_equalTo(kScreenHeight*0.6);
            make.bottom.equalTo(self.view.mas_bottom).offset(10);
        }];
        
    }
    return _bottomView;
}


- (UIView*)wifiListView
{
     if (!_wifiListView) {
         _wifiListView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_wifiListView registerClass:[VL4WiFiCell class] forCellReuseIdentifier:@"VL4WiFiCell"];
         if(self.message_type){
             _wifiListView.separatorStyle =  UITableViewCellSeparatorStyleSingleLine;
             _wifiListView.rowHeight = 80;
         }else{
             _wifiListView.separatorStyle =  UITableViewCellSeparatorStyleNone;
         }
         
         _wifiListView.layer.cornerRadius = 14;
         _wifiListView.dataSource = self;
         _wifiListView.delegate = self;
         _wifiListView.layer.masksToBounds = YES;
         _wifiListView.userInteractionEnabled = YES;
        [self.bottomView addSubview:_wifiListView];
        [_wifiListView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bottomView.mas_left);
            make.right.equalTo(self.bottomView.mas_right);
            make.height.mas_equalTo(kScreenHeight*0.6-130);
            make.bottom.equalTo(self.bottomView.mas_bottom).offset(-80);
        }];
    }
    return _wifiListView;
}





- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    //空白处的范围
    CGRect rect = CGRectMake(0, 0,kScreenWidth,kScreenHeight*0.4 - 40);
    //判断 点（CGPoint）是否在某个范围（CGRect）内
    if (CGRectContainsPoint(rect, point)) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    VL4WiFiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VL4WiFiCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.wifi_dict = self.wifiarray[indexPath.row];
    return cell;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.wifiarray.count;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *wifi_dict = self.wifiarray[indexPath.row];
    if(wifi_dict){
      self.selectedBlock([wifi_dict objectForKey:@"ssid"]);
     [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}


-(UIButton*)cancelButton
{
     if (!_cancelButton) {
          _cancelButton =[UIButton buttonWithType:UIButtonTypeCustom];
         [_cancelButton addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
          _cancelButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:20.0];
        [self.bottomView addSubview:_cancelButton];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.message_type) {
                make.width.mas_equalTo(kScreenWidth/2);
                make.left.equalTo(self.view.mas_left);
            }else{
                make.centerX.equalTo(self.view.mas_centerX);
                make.width.mas_equalTo(280);
            }
            make.height.mas_equalTo(55);
            make.bottom.equalTo(self.bottomView.mas_bottom).offset(-20);
           
        }];
    }
    
    return _cancelButton;
}

- (void)cancelclick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(UIButton*)completeButton
{
     if (!_completeButton) {
          _completeButton = [UIButton buttonWithType:UIButtonTypeCustom];
         [_completeButton addTarget:self action:@selector(completeButtonlclick) forControlEvents:UIControlEventTouchUpInside];
          _completeButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:20.0];
        [self.bottomView addSubview:_completeButton];
        [_completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kScreenWidth/2);
            make.height.mas_equalTo(55);
            make.bottom.equalTo(self.bottomView.mas_bottom).offset(-20);
            make.right.equalTo(self.view.mas_right);
        }];
    }
    
    return _completeButton;
}

- (void)completeButtonlclick
{
    if (self.selected_list_arrar.count == 0) {
        [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"Choose smart lock", nil) duration:1.5];
    }
}

- (UILabel*)titlelabel
{
    if (!_titlelabel) {
         _titlelabel = [[UILabel alloc]init];
        [self.bottomView addSubview:_titlelabel];
         _titlelabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
         _titlelabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16.0];
         _titlelabel.numberOfLines = 0;
         _titlelabel.textAlignment = NSTextAlignmentCenter;
        [_titlelabel sizeToFit];
        [_titlelabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView.mas_top).offset(14);
            make.centerX.equalTo(self.bottomView.mas_centerX);
        }];
   }
    return _titlelabel;
}



@end
